import * as ol from 'ol'
import * as style from 'ol/style'
import * as layer from 'ol/layer'
import * as source from 'ol/source'
import * as geom from 'ol/geom'
import * as proj from 'ol/proj'
import * as format from 'ol/format'
import * as extent from 'ol/extent'
import * as Coordinate from 'ol/coordinate'
/**
 *
 * @param map         地图实例，传了直接定位到中心点最大可视范围，不传返回中心点和最大分辨率
 * @param position    坐标数组
 * @param L           缩放比例系数，数值越小，要素边界越靠近中心
 * @param defaultZoom 只有一组坐标的时候的默认zoom
 */
export const getShapeView = (map, position, L = 600, defaultZoom = 12) => {
  const center = extent.getCenter(extent.boundingExtent(position))
  let x = 0
  let y = 0
  position.forEach(v => {
    if (Math.abs(v[0] - center[0]) > x) {
      x = Math.abs(v[0] - center[0])
    }
    if (Math.abs(v[1] - center[1]) > y) {
      y = Math.abs(v[1] - center[1])
    }
  })
  const resolution = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) / (L / document.body.clientWidth * document.body.clientHeight)
  if (map) {
    if (position.length > 1) {
      map.getView().animate({
        center, resolution
      })
    } else {
      map.getView().animate({
        center, zoom: defaultZoom
      })
    }
  }
  return {
    center, resolution
  }
}